package org.andcool.animation.ease;

/**
 * @author Joosun
 * @since 2011-5-8
 */
public class EaseElastic extends BaseEase
{
	private float mAmplitude;

	private float mPeriod;

	public EaseElastic(final EasingMode pEasingMode)
	{
		super(pEasingMode);

		this.mAmplitude = 0;
		this.mPeriod = 0;
	}

	public EaseElastic(final EasingMode pEasingMode, final float pAmplitude, final float pPeriod)
	{
		super(pEasingMode);

		this.mAmplitude = pAmplitude;
		this.mPeriod = pPeriod;
	}

	public void setAmplitude(final float pAmplitude)
	{
		this.mAmplitude = pAmplitude;
	}

	public float getAmplitude()
	{
		return this.mAmplitude;
	}

	public void setPeriod(final float pPeriod)
	{
		this.mPeriod = pPeriod;
	}

	public float getPeriod()
	{
		return this.mPeriod;
	}

	@Override
	protected float getEaseInInterpolation(float pPercentage)
	{
		float s;

		if (pPercentage == 0)
		{
			return 0;
		}
		if (pPercentage >= 1)
		{
			return 1;
		}
		if (this.mPeriod == 0)
		{
			this.mPeriod = 0.3f;
		}
		if ((this.mAmplitude == 0) || (this.mAmplitude < 1))
		{
			this.mAmplitude = 1;
			s = this.mPeriod / 4;
		}
		else
		{
			s = (float) (this.mPeriod / (2 * Math.PI) * Math.asin(1 / this.mAmplitude));
		}
		return (float) (-(this.mAmplitude * Math.pow(2, 10 * (pPercentage -= 1)) * Math.sin((pPercentage - s) * (2 * Math.PI) / this.mPeriod)));
	}

	@Override
	protected float getEaseOutInterpolation(final float pPercentage)
	{
		float s;

		if (pPercentage == 0)
		{
			return 0;
		}
		if (pPercentage >= 1)
		{
			return 1;
		}
		if (this.mPeriod == 0)
		{
			this.mPeriod = 0.3f;
		}
		if ((this.mAmplitude == 0) || (this.mAmplitude < 1))
		{
			this.mAmplitude = 1;
			s = this.mPeriod / 4;
		}
		else
		{
			s = (float) (this.mPeriod / (2 * Math.PI) * Math.asin(1 / this.mAmplitude));
		}
		return (float) (this.mAmplitude * Math.pow(2, -10 * pPercentage) * Math.sin((pPercentage - s) * (2 * Math.PI) / this.mPeriod) + 1);
	}

	@Override
	protected float getEaseInOutInterpolation(float pPercentage)
	{
		float s;

		if (pPercentage == 0)
		{
			return 0;
		}
		if (pPercentage >= 1)
		{
			return 1;
		}
		if (this.mPeriod == 0)
		{
			this.mPeriod = 0.3f * 1.5f;
		}
		if ((this.mAmplitude == 0) || (this.mAmplitude < 1))
		{
			this.mAmplitude = 1;
			s = this.mPeriod / 4;
		}
		else
		{
			s = (float) (this.mPeriod / (2 * Math.PI) * Math .asin(1 / this.mAmplitude));
		}

		pPercentage *= 2;

		if (pPercentage < 1)
		{
			return (float) (-0.5 * (this.mAmplitude * Math.pow(2, 10 * (pPercentage -= 1)) * Math.sin((pPercentage - s) * (2 * Math.PI) / this.mPeriod)));
		}
		else
		{
			return (float) (this.mAmplitude	* Math.pow(2, -10 * (pPercentage -= 1))	* Math.sin((pPercentage - s) * (2 * Math.PI) / this.mPeriod) * 0.5 + 1);
		}
	}
}